<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class auction extends MY_Controller {
    /* 来自父类
     * public $ceeconfigArray;//cee配置
     * public $userinfoArray; //用户信息
     * public $isloginArray; // 用户是否登录
     */
    //析构
    function __construct(){
        parent::__construct();
        //加载数据库
        $this->load->model('Auction_model','_model');
    }
    //页面信息
	public function index(){
	    //-------1.竞拍商品------//
	    //1.1竞拍商品信息
	    $goodsArray = $this->_model->getGoodsArray();//数组，不止有0：今日商品，还有1：明日商品
	    //1.2轮播的中奖历史
	    $auctionSuc = $this->_model->getauctionSuc();
	    //-------2.兑换商品------//
	    //2.1兑换商品信息
	    $exchangeGoods = $this->_model->getExchangeGoods();
	    //2.2预设兑换时出现的问题
	    $this->_model->setExchangeQuestion();
	    //-------3.是否已经进入竞拍------//
	    $isFirstTimeToAuction = 0;
	    if(!$this->session->userdata('isFirstTimeToAuction')){
	    	$isFirstTimeToAuction = 1;
	    	$this->session->set_userdata('isFirstTimeToAuction', true);
	    }
	    //-------4.视图中的参数------//
	    $data = array('appid' => $this->ceeconfigArray['appid'],
	                    'pf'  => $this->session->userdata('pf')?$this->session->userdata('pf'):"qzone",
	                    'userinfo' => $this->userinfoArray,
	                    'todayGoods' => $goodsArray[0],
	                    'tomorrowGoods' => $goodsArray[1],
	                    'controller' => 'auction',
	                    'auctionSuc' => $auctionSuc,
	    				'exchangeGoods'=>$exchangeGoods,
	    				'isFirstTimeToAuction'=>$isFirstTimeToAuction
	    );
	    $this->load->view('common/header',$data);
		$this->load->view('auction');//加载视图view
		$this->load->view('common/footer');
	}
	/*===========1.竞拍============*/
	//出价
	public function AjaxUserOffer(){
	    $userinfo = $this->userinfoArray;//用户信息
	    //$rs = $this->_model->qiaodao($userinfo['web']);
	    //无参数
	    if (empty($_GET['id']) || empty($_GET['offer'])) {
	    	$rs['ret'] = -1;
	    	$rs['msg'] = "系统繁忙，请刷新操作！";
	    	echo json_encode($rs);
	    	return false;
	    }
	    //有参数
	    $IP = $this->GetIP();
	    $rs = $this->_model->AjaxUserOffer($_GET['id'],$_GET['offer'],$IP,$userinfo);
		echo json_encode($rs);
		return false;
	}
	//提醒
	public function AjaxRemind(){
		//$rs = $this->ceeclass->isReminderSet();
		$rs = $this->ceeclass->setReminder();
		echo json_encode($rs);
		return false;
	}
	/*===========2.兑换============*/
	//获取兑换的问题
	public function AjaxExchange(){
		//无参数
		if (empty($_GET['id']) || !isset($_GET['status'])) {
			$rs['ret'] = -1;
			$rs['msg'] = "参数错误，请刷新操作！";
			echo json_encode($rs);
			return false;
		}
		if ($_GET['status']==-1) {
			echo json_encode(array());
			return false;
		}
		//有参数
		$rs = $this->_model->AjaxExchange($_GET['id'],$_GET['status']);
		echo json_encode($rs);
		return false;
	}
	//开始兑换
	public function AjaxExchange2(){
		//无参数
		if (empty($_GET['id'])) {
			$rs['ret'] = -1;
			$rs['msg'] = "参数错误，请刷新操作！";
			echo json_encode($rs);
			return false;
		}
		//无参数
		if (date("H")<10) {
			$rs['ret'] = -2;
			$rs['msg'] = "还没开始请耐心等待！";
			echo json_encode($rs);
			return false;
		}
		//有参数
		$userinfo = $this->userinfoArray;//用户信息
		$rs = $this->_model->AjaxExchange2($_GET['id'],$userinfo);
		echo json_encode($rs);
		return false;
	}
	/* ----------------------------------------------------------------------------------------
	 * ----------------------------------------免费竞拍历史----------------------------------------
	 * ---------------------------------------------------------------------------------------- */
	//历史页面
	public function auctionHistory(){
		//竞拍历史数据变更
		$this->_model->auctionsuccess();
		//历史数据
		$historyData = $this->_model->auctionHistory();
	    //视图中的参数
	    $data = array(
	    		'appid'       => $this->ceeconfigArray['appid'],
	            'pf'          => $this->session->userdata('pf')?$this->session->userdata('pf'):"qzone",
	            'userinfo'    => $this->userinfoArray,
	            'controller'  => 'auction',
	    		'historyData' => $historyData
	    		//'auctionSuccGoods' => $auctionSuccGoods,
	    		//'exchangeSucc' => $exchangeSucc
	    );
	    $this->load->view('common/header',$data);
	    $this->load->view('auctionHistory');//加载视图view
	    $this->load->view('common/footer');
	}
	/*===========1竞拍============*/
	//1.1出价详情
	public function AjaxAuctionSuccInfo(){
		//无参数
		if (empty($_GET['id'])) {
			$rs['ret'] = -1;
			echo json_encode($rs);
			return false;
		}
		//出价详情
		$auctionSuccInfo = $this->_model->AjaxAuctionSuccInfo($_GET['id']);
		echo json_encode($auctionSuccInfo);
		return false;
	}
	//1.2用户出价详情
	public function AjaxUserAuctionSuccInfo(){
		//无参数
		if (empty($_GET['id'])) {
			$rs['ret'] = -1;
			echo json_encode($rs);
			return false;
		}
		$UserAuctionSuccInfo = $this->_model->AjaxAuctionSuccInfo($_GET['id'],2);
		echo json_encode($UserAuctionSuccInfo);
		return false;
	}
	/*===========2.兑换============*/
	public function AjaxExchangeSuccInfo(){
		//无参数
		if (empty($_GET['id']) || empty($_GET['pdate'])) {
			$rs['ret'] = -1;
			$rs['msg'] = "无响应，请刷新！";
			echo json_encode($rs);
			return false;
		}
		//兑换详情
		$exchangeSuccInfo = $this->_model->AjaxExchangeSuccInfo($_GET['id'],$_GET['pdate']);
		echo json_encode($exchangeSuccInfo);
		return false;
	}
	public function AjaxUserExchangeSuccInfo(){
		//无参数
		if (empty($_GET['id']) || empty($_GET['pdate'])) {
			$rs['ret'] = -1;
			$rs['msg'] = "无响应，请刷新！";
			echo json_encode($rs);
			return false;
		}
		//用户兑换详情
		$UserExchangeSuccInfo = $this->_model->AjaxUserExchangeSuccInfo($_GET['id'],$_GET['pdate']);
		echo json_encode($UserExchangeSuccInfo);
		return false;
	}
	//兑换的问题
	public function AjaxExchangeQuestion(){
		$question = $this->_model->AjaxExchangeQuestion();
		echo json_encode($question);
		return false;
	}
}